<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" size="small" @click="handleSearchList()">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="handleResetSearch()"
        >重置</el-button>
        <el-button style="float:right;margin-right: 5px" size="small" @click="handleRefreshList">刷新</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="问题：">
            <el-input
              v-model="listQuery.title"
              class="input-width"
              placeholder="请输入问题"
            />
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" />
      <span>数据列表</span>
      <el-button class="btn-add" size="mini" @click="handleAdd()" v-if="userId == 1">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="table"
        v-loading="listLoading"
        :data="list"
        style="width: 100%;"
        border
        @selection-change="handleSelectionChange"
      >
        <!--<el-table-column type="selection" width="60" align="center" />-->
        <el-table-column label="编号" align="center" width="80">
          <template slot-scope="scope">{{ scope.$index +1 }}</template>
        </el-table-column>
        <el-table-column label="问题" align="center">
          <template slot-scope="scope">{{ scope.row.title}}</template>
        </el-table-column>
        <el-table-column label="推送对象" align="center">
          <template slot-scope="scope">{{ scope.row.pushObj | pushObjOptions }}</template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="发布状态" align="center" v-if="userId == 1">
          <template slot-scope="scope">{{ scope.row.pushFlag | pushFlagOptions}}</template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleExamine(scope.row)" v-if="userId != 1">查看</el-button>
            <el-button size="mini" @click="handleUpd(scope.row)" v-if="userId == 1">编辑</el-button>
            <el-button size="mini" type="primary" @click="handleIssue(scope.row)" v-if="userId == 1 && scope.row.pushFlag == 0" >发布</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)" v-if="userId == 1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.current"
        :page-size="listQuery.size"
        :page-sizes="[5,10,20,30,50]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <detail-dialog :dialogData="detailDialogData" :isEdit="isEdit" @getList="getList" :addNum="addNum"/>
    <examine-dialog :dialogData="examineDialogData"  />
  </div>
</template>
<script>
import * as api from '@/api/platform-setup'
import DetailDialog from './components/DetailDialog'
import ExamineDialog from './components/ExamineDialog'
import { getId } from '@/utils/auth'

const defaultListQuery = {
  current: 1,
  size: 10,
  title: null
}
export default {
  name: 'versions',
  components: {
    DetailDialog,
    ExamineDialog
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      backQuery: [],
      listLoading: false,
      list: [],
      total: null,
      operateType: null,
      operateOptions: [],
      multipleSelection: [],
      detailDialogData: {
        dialogVisible: false,
        value: '',
        id: ''
      },
      examineDialogData: {
        dialogVisible: false,
        value: '',
        id: ''
      },
      isEdit: false,
      addNum: 0
    }
  },
  watch: {
  },
  computed: {
    userId() {
      return getId()
    }
  },
  created() {
    this.getList()
  },
  filters: {
  },
  methods: {
    handleRefreshList() {
      this.getList()
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
      this.getList()
    },
    handleSearchList() {
      this.listQuery.current = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.listQuery.current = 1
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.current = val
      this.getList()
    },
    /* 获取列表 */
    getList() {
      this.listLoading = true
      console.log(getId())
      if(getId() == 1){
        delete this.listQuery.pushObj
      } else {
        this.listQuery.pushObj = 4
      }
      api
        .helpPage(this.listQuery)
        .then(response => {
          if (response.data.pages>0 && (response.data.pages < this.listQuery.current)) {
            this.listQuery.current = response.data.pages
            this.getList()
            return
          }
          this.listLoading = false
          this.list = response.data.records
          this.total = response.data.total
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    /* 编辑 */
    handleUpd(row) {
      this.detailDialogData.value = row
      this.detailDialogData.dialogVisible = true
      this.isEdit = true
    },/* 发布 */
    handleIssue(row) {
      this.$confirm('确定发布吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = false
        api
          .publishHelp({id:row.id})
          .then(() => {
            this.listLoading = false
            this.$message({
              message: '发布成功！',
              type: 'success',
              duration: 3000
            })
            this.listQuery.current = 1
            this.getList()
          })
          .catch(() => {
            this.listLoading = false
          })
      })
    },
    /* 添加 */
    handleAdd() {
      this.detailDialogData.dialogVisible = true
      this.isEdit = false
      this.addNum +=1
    },
    //    查看
    handleExamine(row){
      this.examineDialogData.value = row
      this.examineDialogData.dialogVisible = true
    },
    /* 删除 */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        api
          .deleteHelp(row.id)
          .then(() => {
            this.listLoading = false
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 3000
            })
            this.getList()
          })
          .catch(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>
